#conding:utf-8

import requests
import csv
import os
import time


# 下载歌曲, 需要创建 music文件夹
def download_music():

    path = 'music/'
    # if os.path.isdir(path):
    #     #     os.mkdir(path)

    with open('music_url.csv', 'r', encoding='utf-8') as f:
        music_li = csv.reader(f)
        for i in music_li:
            audio_name = i[0]
            author_name = i[1]
            song_name = i[2]
            play_url = i[3]
            music_type = play_url.split('.')[-1]
            print(audio_name, play_url, music_type)

            if '' == play_url:
                continue
            try:
                conn = requests.get(play_url)
                with open(path+audio_name+'.'+music_type, 'wb') as f:
                    f.write(conn.content)

                time.sleep(5)
            except Exception as e:
                with open('error.txt', 'a+') as f:
                    f.write(audio_name+'\n')
                    f.write(play_url+'\n')
                    f.write('--------------------------'+'\n')




download_music()